
/* eslint-disable */ 
import { handleRouter } from './handle-router'

let prevRoute = ''
let nextRoute = window.location.pathname

export const getPrevRoute = () => prevRoute
export const getNextRoute = () => nextRoute
export const rewiteRouter = () => {
  window.addEventListener('popstate', () => {
    // 只能监听前进后退
    // console.log('popstate')
    prevRoute = nextRoute
    nextRoute = window.location.pathname
    handleRouter()
  })

  const rawPushState = window.history.pushState
  window.history.pushState = (...args) => {
    //  监听路由切换
    prevRoute = window.location.pathname
    rawPushState.apply(window.history, args)
    nextRoute = window.location.pathname
    // console.log('pushstate变化了')
    handleRouter()
  }

  const rawReplaceState = window.history.replaceState
  window.history.replaceState = (...args) => {
    //  监听路由重置
    prevRoute = window.location.pathname
    rawReplaceState.apply(window.history, args)
    nextRoute = window.location.pathname
    // console.log('replaceState变化了')
    handleRouter()
  }
}
